package com.beiding.controller;

import com.beiding.pojo.room.Room;
import com.beiding.service.email.AuthService;
import com.beiding.service.storage.RoomStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.security.Principal;
import java.util.Collection;

@Controller
public class IndexController {


    private RoomStorageService roomStorageService;

    @Autowired
    public void setRoomService(RoomStorageService roomStorageService) {
        this.roomStorageService = roomStorageService;
    }

    @GetMapping("/")
    public String home(Principal principal) {


        UsernamePasswordAuthenticationToken user = (UsernamePasswordAuthenticationToken) principal;

        final Collection<GrantedAuthority> collection = user.getAuthorities();

        for (GrantedAuthority authority:collection)
        {
            //如果用户具有landlord权限则进入主页
            if (authority.getAuthority().equals("ROLE_LANDLORD")) {
                return "myRooms";
            }
        }

        //如果没有指定权限,则进入协议页面,请用户同意相应的协议再继续
        return "agreement";

    }

    @GetMapping("/put")
    @Secured("ROLE_LANDLORD")
    public String put() {
        return "put";
    }


    @GetMapping("/edit")
    @Secured("ROLE_LANDLORD")
    public String edit(String id, Model model) {
        model.addAttribute("id", id);
        return "edit";
    }

    @GetMapping("/myRooms")
    @Secured("ROLE_LANDLORD")
    public String myRooms() {//我的房源
        return "myRooms";
    }

    @GetMapping("/orders")
    @Secured("ROLE_LANDLORD")
    public String orders() {//我的房源
        return "orders";
    }

    @GetMapping("/roomDetails")
    @Secured("ROLE_LANDLORD")
    public String details(String id, Model model) {
        Room room = roomStorageService.get(id);
        model.addAttribute(room);
        return "roomDetails";
    }


    private AuthService emailService;

    @Autowired
    public void setEmailService(AuthService emailService) {
        this.emailService = emailService;
    }

    @GetMapping("/resend-unlock-email")
    @Secured("ROLE_LANDLORD")
    String resendUnlockEmail(Principal principal, String orderId, Model model) {

        String e = emailService.getEmail(principal.getName());

        String[] split = e.split("@");

        String p = split[0];

        String ph = "";

        int hNum = 0;

        if (p.length() <= 3) {

            //保留第一位,其他隐藏

            ph += p.substring(0, 1);

            hNum = p.length() - 1;

        } else {//如果多于三位
            ph += p.substring(0, p.length() - 3);

            hNum = 3;

        }

        //前缀显示文本
        model.addAttribute("preShow", ph);

        //前缀隐藏字符数
        model.addAttribute("hNum", hNum);

        //后缀
        model.addAttribute("sub", "@" + split[1]);

        model.addAttribute("orderId", orderId);

        return "resendUnlockEmail";
    }


}
